/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Desktop_ViewOrders.java
 *
 * Created on Apr 7, 2011, 1:17:03 PM
 */
package DesktopGUIpackage;

import Controller.DesktopController;
import Controller.DesktopControllerHelper;
import DTO.Order;
import Utilities.MoneyFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

/**
 *
 * @author edaype
 */
public class Desktop_ViewOrders extends javax.swing.JPanel {

    private ArrayList<Order> orderList;
    private DesktopControllerHelper desktopControllerHelper;

    /** Creates new form Desktop_ViewOrders */
    public Desktop_ViewOrders(DesktopControllerHelper desktopControllerHelper, ArrayList<Order> order) {


        this.desktopControllerHelper = desktopControllerHelper;

        System.out.println("orde size from db " + order.size());
        orderList = (ArrayList<Order>) order.clone();
        initComponents();
        initTable();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        ButtonLayout = new javax.swing.JPanel();
        AddButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));

        ButtonLayout.setBackground(new java.awt.Color(255, 255, 255));

        AddButton.setFont(new java.awt.Font("Tahoma", 0, 18));
        AddButton.setText("View Details");
        AddButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddButtonActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Copperplate Gothic Bold", 2, 60));
        jLabel1.setText("Order History");

        javax.swing.GroupLayout ButtonLayoutLayout = new javax.swing.GroupLayout(ButtonLayout);
        ButtonLayout.setLayout(ButtonLayoutLayout);
        ButtonLayoutLayout.setHorizontalGroup(
            ButtonLayoutLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ButtonLayoutLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 508, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 102, Short.MAX_VALUE)
                .addComponent(AddButton, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        ButtonLayoutLayout.setVerticalGroup(
            ButtonLayoutLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, ButtonLayoutLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(ButtonLayoutLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(AddButton, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addContainerGap())
        );

        jTable1.setFont(new java.awt.Font("Tahoma", 0, 14));
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Order ID", "Order Date", "Total Paid"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(0).setHeaderValue("Order ID");
        jTable1.getColumnModel().getColumn(1).setHeaderValue("Order Date");
        jTable1.getColumnModel().getColumn(2).setHeaderValue("Total Paid");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 16));
        jLabel2.setText("To view a detailed description of an order");

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 16));
        jLabel3.setText("select from table and click View Details");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(10, Short.MAX_VALUE)
                .addComponent(ButtonLayout, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(10, 10, 10))
            .addGroup(layout.createSequentialGroup()
                .addGap(124, 124, 124)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 534, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(142, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addComponent(jLabel3)
                .addGap(40, 40, 40)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 350, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
                .addComponent(ButtonLayout, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void AddButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddButtonActionPerformed
        // TODO add your handling code here:
        //CODE TO DISPLAY ORDER DETAILS
        int row = jTable1.getSelectedRow();
        if (row != -1){
            System.out.println("SelectedRow  = " + row );
        Order Order = orderList.get(row);
        desktopControllerHelper.ItemHistoryScreen(Order);
        }


    }//GEN-LAST:event_AddButtonActionPerformed

    private void initTable() {
        //CODE TO DIPLAY ORDERS
        MoneyFormatter formatter = new MoneyFormatter();
        ListIterator<Order> itr;
        Object[][] obj = null;
        int index;
        Order order;
        if (orderList.size() != 0) {
            obj = new Object[orderList.size()][3];
            itr = orderList.listIterator();
            while (itr.hasNext()) {
                index = itr.nextIndex();
                order = itr.next();
                obj[index][0] = order.getID();
                obj[index][1] = order.getOrderDate();
                obj[index][2] = formatter.format(order.getItemSubTotal() + order.getTaxAmount());
            }
        }

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
                // new Object [][] {
                obj,
                //},
                new String[]{
                    "Order ID", "Order Date", "Total Paid"
                }) {

            boolean[] canEdit = new boolean[]{
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AddButton;
    private javax.swing.JPanel ButtonLayout;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
}
